﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.Utility;
using System.IO;
using System.Xml;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Common.ControlDevice
{
    public class Settings
    {
        // 初始化打开时是否全屏 2021.05.07 wj 增
        private bool isInitFullScreen = false;
        public bool IsInitFullScreen
        {
            get { return isInitFullScreen; }
            set
            {
                isInitFullScreen = value;
                OnNotifyPropertyChanged("IsInitFullScreen");
            }
        }

        // 日志保存路径 2020.12.04 wj 增
        private String recordLogPath = String.Empty;
        public String RecordLogPath
        {
            get { return recordLogPath; }
            set
            {
                recordLogPath = value;
                OnNotifyPropertyChanged("RecordLogPath");
            }
        }

        // 开发者模式，默认为否 2020.12.04 wj 增
        private bool isDeveloperMode = false;
        public bool IsDeveloperMode
        {
            get { return isDeveloperMode; }
            set
            {
                isDeveloperMode = value;
                OnNotifyPropertyChanged("IsDeveloperMode");
            }
        }

        // 初始化绘图时，是否对设备进行自动命名，默认为否 2021.04.01 wj 增
        private bool isInitCreateDeviceName = false;
        public bool IsInitCreateDeviceName
        {
            get { return isInitCreateDeviceName; }
            set
            {
                isInitCreateDeviceName = value;
                OnNotifyPropertyChanged("IsInitCreateDeviceName");
            }
        }

        // 是否开启自动保存 2020.12.21 wj 增
        private bool isAutoSave = true;
        public bool IsAutoSave
        {
            get { return isAutoSave; }
            set
            {
                isAutoSave = value;
                OnNotifyPropertyChanged("IsAutoSave");
            }
        }

        // 自动保存时长 2020.12.21 wj 增
        private uint autoSaveTime = 5;
        public uint AutoSaveTime
        {
            get { return autoSaveTime; }
            set
            {
                if (value >= 3 && value <= 60)
                {
                    autoSaveTime = value;
                }
                else
                {
                    autoSaveTime = 5;
                }
                OnNotifyPropertyChanged("AutoSaveTime");
            }
        }

        // 相关表格信息 2021.03.03 wj 增
        private ObservableCollection<SelectedDevice> excelVersionCollection = new ObservableCollection<SelectedDevice>();
        public ObservableCollection<SelectedDevice> ExcelVersionCollection
        {
            get { return excelVersionCollection; }
            set
            {
                excelVersionCollection = value;
                OnNotifyPropertyChanged("ExcelVersionCollection");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        /**
         * \brief 属性更改事件触发处理
         * 
         *  \param [in] PropertyName 属性名
         *  \retval void
         */
        protected void OnNotifyPropertyChanged(string propertyName)
        {
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /**
         * \brief 导入config配置文件
         * \param filePath 配置文件路径
         * \param errorList 错误信息
         * \retval bool 是否导入成功
         */
        public bool ImportConfig(String filePath, List<ErrorInfo> errorList)
        {
            bool res = true;
            using (StreamReader sr = File.OpenText(filePath))
            {
                try
                {
                    JObject jObject = (JObject)JToken.ReadFrom(new JsonTextReader(sr));
                    IsInitFullScreen = jObject["IsInitFullScreen"].ToObject<bool>();
                    IsAutoSave = jObject["IsAutoSave"].ToObject<bool>();
                    AutoSaveTime = jObject["AutoSaveTime"].ToObject<uint>();
                    IsInitCreateDeviceName = jObject["IsInitCreateDeviceName"].ToObject<bool>();
                    ExcelVersionCollection.Clear();
                    foreach (JToken v in jObject["ExcelVersionData"].Children())
                    {
                        ExcelVersionCollection.Add(v.ToObject<SelectedDevice>());
                    }
                }
                catch (Exception ex)
                {
                    errorList.Add(new ErrorInfo(ErrorLevelCategory.Error, "工具配置文件config.json读取失败" + ex.ToString()));
                    return false;
                }
            }
            return res;
        }

        /**
         * \brief 导出config配置文件
         * \retval void 空
         */
        public bool ExportConfig(String filePath, List<ErrorInfo> errorList)
        {
            bool res = true;
            try
            {
                using (StreamWriter sw = File.CreateText(filePath))
                {
                    using (JsonTextWriter jsonTextWriter = new JsonTextWriter(sw))
                    {
                        jsonTextWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
                        jsonTextWriter.WriteStartObject();

                        jsonTextWriter.WritePropertyName("IsInitFullScreen");
                        jsonTextWriter.WriteValue(IsInitFullScreen);
                        jsonTextWriter.WritePropertyName("IsAutoSave");
                        jsonTextWriter.WriteValue(IsAutoSave);
                        jsonTextWriter.WritePropertyName("AutoSaveTime");
                        jsonTextWriter.WriteValue(AutoSaveTime);
                        jsonTextWriter.WritePropertyName("IsDevelopMode");
                        jsonTextWriter.WriteValue(IsDeveloperMode);
                        jsonTextWriter.WritePropertyName("IsInitCreateDeviceName");
                        jsonTextWriter.WriteValue(IsInitCreateDeviceName);

                        jsonTextWriter.WritePropertyName("ExcelVersionData");
                        jsonTextWriter.WriteStartArray();
                        foreach (SelectedDevice v in ExcelVersionCollection)
                        {
                            jsonTextWriter.WriteStartObject();
                            jsonTextWriter.WritePropertyName("FilePath");
                            jsonTextWriter.WriteValue(v.FilePath);
                            jsonTextWriter.WritePropertyName("FileVersion");
                            jsonTextWriter.WriteValue(v.FileVersion);
                            jsonTextWriter.WriteEndObject();
                        }
                        jsonTextWriter.WriteEndArray();
                        jsonTextWriter.WriteEndObject();
                    }
                }
            }
            catch(Exception ex)
            {
                errorList.Add(new ErrorInfo(ErrorLevelCategory.Error, "工具配置文件config.json导出失败" + ex.ToString()));
                return false;
            }
            return res;
        }
    }
}
